﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DataIntegrityChecker.Data
{
    public class DataManager
    {
        private string[] headers;
        private List<DataRow> rows;
        private bool firstRowHeader;

        public DataManager()
        {
            rows = new List<DataRow>();
        }

        public string[] Headers
        {
            get { return headers; }
            set { headers = value; }
        }

        internal List<DataRow> Rows
        {
            get { return rows; }
        }

        public void LoadData(string inputFile, bool firstRowHeader)
        {
            try
            {
                rows.Clear(); 
                StreamReader reader = new StreamReader(inputFile, Encoding.UTF8);
                int maxColumns = 0;
                int rowCount =0;
                if (firstRowHeader)
                {
                    string headerLine = reader.ReadLine();
                    headers = headerLine.Split('\t');
                }
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] values = line.Split('\t');
                    maxColumns = Math.Max(maxColumns, values.Length);
                    DataRow row = new DataRow(rowCount, values);
                    rows.Add(row);
                    rowCount++;
                }
                if (!firstRowHeader)
                {
                    headers = new string[maxColumns];
                    for (int i = 0; i < maxColumns; i++)
                    {
                        headers[i] = "Column " + (i+1);
                    }
                }
                this.firstRowHeader = firstRowHeader;
            }
            catch (Exception e)
            {
                throw new LoadDataException("Loading the data file failed", e);
            }
        }

        public void SaveData(string outputFile)
        {
            try
            {
                StreamWriter writer = new StreamWriter(outputFile, false, Encoding.UTF8);
                if (firstRowHeader)
                {
                    string rowString = "";
                    bool firstColumn = true;
                    foreach (String value in headers)
                    {
                        rowString += (!firstColumn ? "\t" : "") + value;
                        firstColumn = false;
                    }
                    writer.WriteLine(rowString);
                }
                foreach (DataRow row in rows)
                {
                    string rowString = "";
                    bool firstColumn = true;
                    foreach (String value in row.Values)
                    {
                        string columnValue = value;
                        if (columnValue.Contains(",") && !columnValue.StartsWith("\""))
                            columnValue = "\"" + columnValue;
                        if (columnValue.Contains(",") && !columnValue.EndsWith("\""))
                            columnValue = columnValue + "\"";
                        rowString += (!firstColumn ? "\t" : "") + columnValue;
                        firstColumn = false;
                    }
                    writer.WriteLine(rowString);
                }
                writer.Flush(); writer.Close();
            }
            catch (Exception e)
            {
                throw new SaveDataException("Saving the data failed", e);
            }
        }
    }
}
